<?php

namespace App\Http\Controllers\system;

use App\Http\Controllers\Controller;

class RoleController extends Controller
{
    public function index()
    {
        $menu_id = request('curr_menu_id', 0);
        if(!$menu_id){
            return response()->json('非法操作');
        }
        $toolBarButton = $this->buildToolBarButton($menu_id);
        return view('system.role.list',compact('menu_id','toolBarButton'));
    }

    public function listData()
    {
        $params['curr_menu_id'] = request('curr_menu_id', 0);
        if(!$params['curr_menu_id']){
            return response()->json(['code'=>400, '页面无菜单ID']);
        }
        $params['page'] = request('page', 1);
        $params['rows'] = request('rows', 10);
        try{
            $list = callApiServer("System.RoleService.getInfoList", $params);
            return response()->json($list);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function showAddForm()
    {
        return view('system.role.form');
    }

    public function showEditForm()
    {
        $params['id'] = request('role_id');
        try{
            $info = callApiServer("system.RoleService.getInfoById", $params);
            if(empty($info)){
                return response()->json('对应数据不存在');
            }
            return view('system.role.form', compact("info"));
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>500,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function save()
    {
        $params['id'] = request('id',0);
        $params['name'] = request('name');
        $params['description'] = request('description',0);
        $params['permission'] = request('permission');
        $params['created_by'] = request('created_by',0);
        $params['is_active'] = request('is_active','1');
        if(empty($params['name'])){
            return response()->json(['code'=>400,'data'=>'角色名称不能为空']);
        }
        if(empty($params['permission'])){
            return response()->json(['code'=>400,'data'=>'角色权限不能为空']);
        }
        $rs = callApiServer("System.RoleService.saveData", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>$rs]);
    }

    public function roleAssign(){
        $params['id'] = request('role_id');
        try{
            $arr = callApiServer("system.RoleService.getInfoList1");
            $arr1 = callApiServer("system.RoleService.roleCheck",$params);
            $arr2 = callApiServer("system.RoleService.roleCheck1",$params);
            if(empty($arr)){
                return response()->json('对应数据不存在');
            }
            $role_id = $params['id'];
            return view('system.role.form1', compact("arr","arr1","arr2","role_id"));
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>500,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function delAdd()
    {
        $params['id'] = request('id',0);
        $params['checkbox'] = request('checkbox');

        $rs = callApiServer("System.RoleService.delAdd", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>$rs]);
    }
}
